package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Warrant;
import com.bdth.bdthiss.mapper.WarrantMapper;
import com.bdth.bdthiss.service.WarrantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LGP
 * @since 2020-04-14
 */
@Service
public class WarrantServiceImpl extends ServiceImpl<WarrantMapper, Warrant> implements WarrantService {

    @Override
    public Page<Warrant> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<Warrant>  entityWrapper = new EntityWrapper<>();
        Object companyname = query.get("companyname");
        Object brandname = query.get("brandname");
        Object region = query.get("region");
        if(companyname!=null && !companyname.toString().equals("")) {
            entityWrapper.like("companyname",companyname.toString());
        }

        if(brandname!=null && !brandname.toString().equals("")) {
            entityWrapper.like("brandname",brandname.toString());
        }

        if(region!=null && !region.toString().equals("")) {
            entityWrapper.like("region",region.toString());
        }

        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Warrant> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Warrant> items = baseMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }
}
